package cn.tedu.boot22.controller;

import cn.tedu.boot22.entity.Hero;
import cn.tedu.boot22.mapper.HeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class HeroController {
    /**
     * 自动注入/自动装配注解作用:Spring框架结合mybatis框架会自动的将HeroMapper生成一个实现类并实现内部方法,
     * 并且会自动实例化此对象,交给Spring管理管理
     * required = false告诉idea编译器此对象是非必须的
     */
    @Autowired(required = false)
    HeroMapper mapper;

    @RequestMapping("add")
    @ResponseBody
    public String add(Hero hero) {
        //此方法相当于之前执行的jdbc代码
        mapper.insert(hero);
        return "添加成功!";
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(String name) {
        //调用mapper中删除的方法
        mapper.deleteByName(name);
        return "删除成功!";
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Hero hero) {
        mapper.update(hero);
        return "修改成功!";
    }

    @RequestMapping("select")
    @ResponseBody
    public String select() {
        List<Hero> list = mapper.select();
        return list.toString();
    }
}
